<template>
    <div>
        <avue-form ref="form" v-model="obj" :option="option" @reset-change="emptytChange" @submit="submit">

            <!-- 菜单树 -->
            <template slot-scope="scope" slot="menuIds">
                <el-tree
                    :data="menuTreeData"
                    show-checkbox
                    node-key="id"
                    @check="menusCheck"
                    :default-expand-all=false
                    :default-checked-keys="menuDefaultCheckedKeys"
                    :props="menusProps">
                </el-tree>
            </template>


            <!-- 权限树 -->
            <template slot-scope="scope" slot="authIds">
                <el-tree
                    :data="authTreeData"
                    show-checkbox
                    node-key="id"
                    @check="authsCheck"
                    :default-expand-all=false
                    :default-checked-keys="authDefaultCheckedKeys"
                    :props="{children: 'children', label: 'name'}">
                    <!-- 自定义节点内容 -->
                    <span slot-scope="{ node, data }">
                         <span v-if="data.pid !=='0' && data.id !=='0'">
                              {{ data.method }} --> {{ data.name }}
                         </span>
                         <span v-if="data.pid ==='0' || data.id ==='0'">
                              {{ data.name }}
                         </span>
                    </span>
                </el-tree>
            </template>

        </avue-form>
    </div>
</template>

<script>
export default {
    // name: "RoleUpd",
    data() {
        return {
            obj: this.rowData,
            sizeValue: 'small',
            // 菜单配置
            menuTreeData: [],
            authTreeData: [],
            menuDefaultCheckedKeys: [],
            authDefaultCheckedKeys: [],
            menusProps: {
                children: 'menus',
                label: 'name'
            },
        }
    },
    // 接收值父组件传递值
    props: {
        closeDialog: [],         // 关闭弹层方法
        uri: {},                 // 添加接口
        rowData: {},             // 当前行数据
    },
    computed: {
        option() {
            return {
                size: this.sizeValue,
                submitText: '提交',
                emptyText: "关闭",
                submitBtn: true,   // 提交按钮
                emptyBtn: true,    // 清空按钮
                mockBtn: false,    // 模拟按钮
                printBtn: false,   // 打印按钮
                column: [
                    {
                        label: "角色名称",
                        prop: "name",
                        // tip: '这是信息提示',
                        span: 20,
                        maxlength: 32,
                        showWordLimit: true,
                        rules: [{
                            required: true,
                            message: "请输入 角色名称",
                            trigger: "blur"
                        }],
                    },
                    {
                        label: "code",
                        prop: "code",
                        span: 20,
                        maxlength: 32,
                        showWordLimit: true,
                        rules: [{
                            required: true,
                            message: "请输入 角色code",
                            trigger: "blur"
                        }],
                    },
                    {
                        label: "描叙",
                        prop: "desc",
                        span: 20,
                        maxlength: 256,
                        showWordLimit: true,
                        rules: [{
                            required: true,
                            message: "请输入 角色code",
                            trigger: "blur"
                        }],
                    },
                    // {
                    //     label: "是否禁用",
                    //     prop: "disable",
                    //     span: 20,
                    //     type: "switch",
                    //     dicData: getDict(website.Dict.Base.Disable, false),
                    // },
                    {
                        label: '菜单权限',
                        prop: 'menuIds',
                        span: 10,
                        rules: [{
                            required: false,
                            message: "请选择 菜单",
                            trigger: "blur"
                        }],
                    }, {
                        label: '资源权限',
                        prop: 'authIds',
                        span: 10,
                        rules: [{
                            required: false,
                            message: "请选择 权限",
                            trigger: "blur"
                        }],
                    }
                ]
            }
        }
    },
    async created() {
        // 获取菜单选中数据
        this.obj = this.rowData;

        // 获取菜单数据
        let menuRes = await this.crud.get(this.uri.findMenuTree, {disable: 0});
        this.menuTreeData = menuRes.data.data;
        // 获取当前选中数据 (只设置页面层数据 root=3)
        let roleMenuRes = await this.crud.get(this.uri.findMenuIdsById, {id: this.obj.id});
        this.obj.menuIds = roleMenuRes.data.data.map(menu => menu.id);
        this.menuDefaultCheckedKeys = roleMenuRes.data.data.filter(menu => menu.root === 3).map(menu => menu.id);

        // 获取权限数据
        let authRes = await this.crud.get(this.uri.findAuthTree, {disable: 0});
        this.authTreeData = authRes.data.data;
        // 获取当前选中数据
        let roleAuthRes = await this.crud.get(this.uri.findAuthIdsById, {id: this.obj.id});
        this.obj.authIds = roleAuthRes.data.data.map(auth => auth.id);
        this.authDefaultCheckedKeys = roleAuthRes.data.data.filter(auth => auth.pid !== "0").map(auth => auth.id);
    },

    /**
     * 请求方法
     * @author wangsong
     * @param null
     * @date 2021/10/10 0010 12:55
     * @return
     * @version 1.0.1
     */
    methods: {
        emptytChange() {
            this.closeDialog(false);
            this.obj = this.rowData;
            this.menuDefaultCheckedKeys = [];
            this.authDefaultCheckedKeys = [];
        },
        submit(form, done) {
            this.crud.put(this.uri.info + "/" + this.obj.id, this.obj).then((res) => {
                // 添加成功关闭弹层
                if (res.data.code == 200) {
                    this.closeDialog(true);
                }
                done(form);
            }).catch((err) => {
                console.error(err);
                done(form);
            })
        },
        // 共两个参数，
        // 依次为：传递给 data 属性的数组中该节点所对应的对象、
        // 树目前的选中状态对象，包含 checkedNodes、checkedKeys、halfCheckedNodes、halfCheckedKeys 四个属性
        menusCheck(data, nodes) {
            this.obj.menuIds = [];
            this.obj.menuIds.push.apply(this.obj.menuIds, nodes.checkedKeys);     // 全选状态数据
            this.obj.menuIds.push.apply(this.obj.menuIds, nodes.halfCheckedKeys); // 半选状态数据
            console.debug("menuIds=", this.obj.menuIds)
        },
        //
        authsCheck(data, nodes) {
            this.obj.authIds = [];
            this.obj.authIds.push.apply(this.obj.authIds, nodes.checkedKeys);     // 全选状态数据
            this.obj.authIds.push.apply(this.obj.authIds, nodes.halfCheckedKeys); // 半选状态数据
            // 过滤等于0的数据
            this.obj.authIds = this.obj.authIds.filter(id => id !== '0');
            console.debug("authIds=", this.obj.authIds)
        },
    }
}
</script>
